﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.IO;
using System.Web;
using System.Threading.Tasks;
using System.Data;
using BartonExtension;
using ResponseObject;
using iShangMai.Models;

namespace GreatShots.Controllers
{
    public class FileUploadController : ApiController
    {
        //
        // GET: /FileUpload/

        [HttpPost]
        public async Task<ZKResponse> FileUpload()
        {
            ZKResponse response = new ZKResponse();
            try
            {
                if (!Request.Content.IsMimeMultipartContent("form-data"))
                {
                    response.isProcessSuccess = false;
                    response.errMsg = "参数非法";
                    return response;
                }
                var pathID = DateTime.Now.DateTimeFileName();
                var root = HttpContext.Current.Server.MapPath("~/userdata/imgpool/{0}".FormatWith(pathID));
                if (!Directory.Exists(root))
                    Directory.CreateDirectory(root);
                var provider = new RenamingMultipartFormDataStreamProvider(root);
                var body = await Request.Content.ReadAsMultipartAsync(provider);
                long filelength = FileIO.FileLength(body.FileData.First().LocalFileName);
                List<UploadImageEntity> files = new List<UploadImageEntity>();
                if (filelength > 0)
                {
                    foreach (var savedName in body.FileData)
                    {
                        UploadImageEntity entity = new UploadImageEntity();
                        entity.ori_image_url = ConfigurationExtensions.ValueOfConfig("host_name") +
                                               "/userdata/imgpool/{0}/{1}".FormatWith(pathID,Path.GetFileName(savedName.LocalFileName));
                        var thumbPath = HttpContext.Current.Server.MapPath("~/userdata/thumbspool/{0}".FormatWith(pathID));
                        if(!Directory.Exists(thumbPath))
                        {
                            Directory.CreateDirectory(thumbPath);
                        }
                        var big_image_path = thumbPath + "\\big_" + Path.GetFileName(savedName.LocalFileName);
                        if(ImageHelper.GetFixedRatioImage(savedName.LocalFileName,big_image_path,750,int.MaxValue))
                        {
                            entity.big_thumb_image_url = ConfigurationExtensions.ValueOfConfig("host_name") + "/userdatauserdata/thumbspool/{0}/{1}".FormatWith(pathID, Path.GetFileName(big_image_path));
                        }
                        else
                        {
                            entity.big_thumb_image_url = "err";
                        }

                        var medium_image_path = thumbPath + "\\medium_" + Path.GetFileName(savedName.LocalFileName);
                        if (ImageHelper.GetFixedRatioImage(savedName.LocalFileName, medium_image_path, 320, int.MaxValue))
                        {
                            entity.medium_thumb_image_url = ConfigurationExtensions.ValueOfConfig("host_name") + "/userdatauserdata/thumbspool/{0}/{1}".FormatWith(pathID, Path.GetFileName(medium_image_path));
                        }
                        else
                        {
                            entity.medium_thumb_image_url = "err";
                        }

                        var tiny_thumb_path = thumbPath + "\\small_" + Path.GetFileName(savedName.LocalFileName);
                        if (ImageHelper.GetFixedRatioImage(savedName.LocalFileName, tiny_thumb_path, 150, int.MaxValue))
                        {
                            entity.tiny_thumb_image_url = ConfigurationExtensions.ValueOfConfig("host_name") + "/userdatauserdata/thumbspool/{0}/{1}".FormatWith(pathID, Path.GetFileName(tiny_thumb_path));
                        }
                        else
                        {
                            entity.tiny_thumb_image_url = "err";
                        }
                    }
                }
            }
            catch (Exception e)
            {
                response.isProcessSuccess = false;
                response.errMsg = e.Message;
            }
            return response;
        }
    }
}
